package com.xq.demo;

import com.sun.prism.shader.Solid_TextureYV12_AlphaTest_Loader;

import java.util.concurrent.*;

public class ThreadPoolDemo {


    public static void main(String[] args) {
        //CPU密集度,2*CPU核数
        //IO密集度，4*CPU核数
        int num = Runtime.getRuntime().availableProcessors();
        System.out.println(num);


       ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 10000000,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(5));

        //核心线程数，最大线程数，队列
        executor.submit(new Task("任务1"));
        executor.submit(new Task("任务2"));
        executor.submit(new Task("任务3"));
        executor.submit(new Task("任务4"));
        executor.submit(new Task("任务5"));
        executor.submit(new Task("任务6"));
        executor.submit(new Task("任务7"));

        /* executor.submit(new Task("任务6"));*/
        //int num = Runtime.getRuntime().availableProcessors();
        //System.out.println(num);

       //单一线程池(始终都只有一个线程，实际不会这么用)
       /* ExecutorService executorService = Executors.newSingleThreadExecutor();
        for(int i=0;i<100;i++){
            final int temp = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+",i:"+temp);
                };
            });
        }
        executorService.shutdown();*/


        //可缓存线程池(无限制线程大小,弊端：int的最大值，无限创建线程，浪费资源，有系统崩溃风险)
      /* ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=0;i<10000;i++){
            final int temp = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+",i:"+temp);
                };
            });
        }
        executorService.shutdown();*/

        //定长线程池(自定义设置线程池大小，稍微用的多一点)
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for(int i=0;i<100;i++){
            final int temp = i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+",i:"+temp);
                };
            });
        }
        executorService.shutdown();
        //定时线程池(可以指定时间跑任务，基本不会用到)
      /* ScheduledExecutorService scheduledExecutorService =  Executors.newScheduledThreadPool(3);
        for(int i=0;i<100;i++){
            final int temp = i;
            scheduledExecutorService.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+",i:"+temp);
                };
            },3, TimeUnit.SECONDS);
        }*/


    }

    static class Task implements Runnable{
        private String name;
        public  Task(String name){
            this.name=name;
        }
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+name);
        }
    }

}
